package com.tsd.base.controller;

import com.tsd.CommonParamNameConstants;
import com.tsd.base.entity.BaseUnit;
import com.tsd.base.entity.BaseUnitExt;
import com.tsd.base.service.BaseUnitService;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.controller.SystemBaseController;
import com.tsd.system.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：BaseUnitbase_unit 控制层
 *
 * @author zsy
 * @date 2020/12/29 15:22:59
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "base_unit-base_unit", description = "base_unit的API描述")
public class BaseUnitController extends SystemBaseController {

    @Resource
    private BaseUnitService baseUnitService;

    @ApiOperation(value = "根据sids批量删除", notes = "根据sids批量删除")
    @ResponseBody
    @RequestMapping(value = "/batchDelUnit.action", method = RequestMethod.POST)
    public HttpResult<Object> batchDelUnit() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            List<String> sids = super.getParamsList(map, CommonParamNameConstants.SIDS, String.class, true);
            baseUnitService.batchDeleteUnit(sids, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存base_unit", notes = "保存base_unit，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "base_unit", required = true, paramType = "body", dataType = "BaseUnit")
    })
    @ResponseBody
    @RequestMapping(value = "/saveBaseUnit.action", method = RequestMethod.POST)
    public HttpResult<Object> saveBaseUnit() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            BaseUnit unit = super.getParams(request, BaseUnit.class);
            BaseUnit result = baseUnitService.saveBaseUnit(unit, opUser);
            hr.setData(result);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询base_unit", notes = "分页查询base_unit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findBaseUnits.action", method = RequestMethod.POST)
    public HttpResult<Object> findBaseUnits() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>();
            HlpUtils.copyIntegerValue(map, params, CommonParamNameConstants.DR, false, 0);
            HlpUtils.copyIntegerValue(map, params, CommonParamNameConstants.STATE, false, 1);
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.SELECTED_VALUE, false, false);
            HlpUtils.copyStringValue(map, params, CommonParamNameConstants.SEARCH_VALUE, false, true);
            PageBean page = new PageBean(map).procNoPage(map);
            List<BaseUnitExt> list = baseUnitService.findBaseUnits(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

}
